package com.definesys.smecrailtransit.service.LogService;

import android.content.Context;


/**
 * Created by xupeizuo on 2017/3/3.
 */

public class CrashHandler implements Thread.UncaughtExceptionHandler {

    private static final String TAG=CrashHandler.class.getSimpleName();

    private Thread.UncaughtExceptionHandler mDefaultHandler;
    private static CrashHandler crashHandler;
    private Context mContext;

    private CrashHandler() {
    }

    public static synchronized CrashHandler getCrashHandler(){
        if(crashHandler == null){
            crashHandler=new CrashHandler();
        }
        return crashHandler;
    }

    public void init(Context context){

        mContext=context;
        // 获取系统默认的UncaughtException处理器
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        // 设置该CrashHandler为程序的默认处理器
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {

        if(ex == null){
            return;
        }
        ex.printStackTrace();
        AbnormalCollection.getAbnormalCollection().saveExceptionMsg(ex,AbnormalCollection.EXCEPTION_TYPE_CRASH);

        mDefaultHandler.uncaughtException(thread,ex);
    }
}
